import { Router, RequestHandler, Response } from 'express';
import { auth } from '../middleware/auth';
import { roleAuth } from '../middleware/roleAuth';
import { asyncHandler } from '../middleware/asyncHandler';
import { AnalysisController } from '../controllers/analysisController';
import { TypedRequest } from '../middleware/routeHandler';

// 显式类型注解
const router: Router = Router();

/**
 * 分析相关路由
 * @swagger
 * tags:
 *   name: Analysis
 *   description: 数据分析相关接口
 */

// 所有路由需要认证
router.use(auth as RequestHandler);

// 开始定义路由处理器
type ControllerHandler = RequestHandler;

/**
 * @swagger
 * /api/analysis/summary:
 *   get:
 *     summary: 获取分析总览
 *     tags: [Analysis]
 */
router.get(
  '/summary',
  roleAuth(['teacher', 'admin']) as ControllerHandler,
  asyncHandler(AnalysisController.getAnalysisSummary) as ControllerHandler
);

/**
 * @swagger
 * /api/analysis/evaluation/{id}:
 *   get:
 *     summary: 获取评估分析
 *     tags: [Analysis]
 */
router.get(
  '/evaluation/:id',
  roleAuth(['teacher', 'admin']) as ControllerHandler,
  asyncHandler(AnalysisController.getEvaluationAnalysis) as ControllerHandler
);

/**
 * @swagger
 * /api/analysis/student/{id}/progress:
 *   get:
 *     summary: 获取学生进度分析
 *     tags: [Analysis]
 */
router.get(
  '/student/:id/progress',
  roleAuth(['teacher', 'admin']) as ControllerHandler,
  asyncHandler(AnalysisController.getStudentProgress) as ControllerHandler
);

/**
 * @swagger
 * /api/analysis/class/{id}:
 *   get:
 *     summary: 获取班级整体分析
 *     tags: [Analysis]
 */
router.get(
  '/class/:id',
  roleAuth(['teacher', 'admin']) as ControllerHandler,
  asyncHandler(AnalysisController.getClassAnalysis) as ControllerHandler
);

/**
 * @swagger
 * /api/analysis/question/{id}/difficulty:
 *   get:
 *     summary: 获取问题难度分析
 *     tags: [Analysis]
 */
router.get(
  '/question/:id/difficulty',
  roleAuth(['teacher', 'admin']) as ControllerHandler,
  asyncHandler(AnalysisController.getQuestionDifficultyAnalysis) as ControllerHandler
);

/**
 * @swagger
 * /api/analysis/knowledge-points:
 *   get:
 *     summary: 获取知识点掌握分析
 *     tags: [Analysis]
 */
router.get(
  '/knowledge-points',
  roleAuth(['teacher', 'admin']) as ControllerHandler,
  asyncHandler(AnalysisController.getKnowledgePointsAnalysis) as ControllerHandler
);

/**
 * @swagger
 * /api/analysis/wrong-questions:
 *   get:
 *     summary: 获取错题分析
 *     tags: [Analysis]
 */
router.get(
  '/wrong-questions',
  roleAuth(['teacher', 'admin', 'student']) as ControllerHandler,
  asyncHandler(AnalysisController.getWrongQuestionsAnalysis) as ControllerHandler
);

/**
 * @swagger
 * /api/analysis/time-distribution:
 *   get:
 *     summary: 获取时间分布分析
 *     tags: [Analysis]
 */
router.get(
  '/time-distribution',
  roleAuth(['teacher', 'admin']) as ControllerHandler,
  asyncHandler(AnalysisController.getTimeDistributionAnalysis) as ControllerHandler
);

/**
 * @swagger
 * /api/analysis/export:
 *   post:
 *     summary: 导出分析报告
 *     tags: [Analysis]
 */
router.post(
  '/export',
  roleAuth(['teacher', 'admin']) as ControllerHandler,
  asyncHandler(AnalysisController.exportAnalysisReport) as ControllerHandler
);

// 对所有路由处理函数进行类似修改
router.get('/analysis1', asyncHandler(async (req: TypedRequest, res: Response) => {
  // ... 现有代码 ...
}));

router.get('/analysis2', asyncHandler(async (req: TypedRequest, res: Response) => {
  // ... 现有代码 ...
}));

export default router;
